<?php
/**
 * Created by PhpStorm.
 * User: syzhou
 * Date: 2020-11-26
 * Time: 11:17
 */

namespace App\Console\Commands\Scripts;

require_once __DIR__ . '/../../../Utils/file.php';

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;


class ExportAwardedProgressProjects extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'scripts:export-awarded-progress-projects';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $rawAwardedProjectsFile = __DIR__ . '/input/2020年度中国公路建设行业协会科学技术奖进步奖公示.csv';
        $rawProjects = read_csv_file($rawAwardedProjectsFile);
        $rawHeaders = array_shift($rawProjects);
        $outputHeaders = array_merge($rawHeaders, ['牵头申报单位', '主要完成单位', '主要完成单位数量', '主要完成人', '主要完成人数量']);
        $outputItems = [$outputHeaders];
        $completeSubjectCountLimits = [
            '特等奖' => ['companies' => 10, 'people' => 25],
            '一等奖' => ['companies' => 10, 'people' => 25],
            '二等奖' => ['companies' => 8, 'people' => 15],
            '三等奖' => ['companies' => 3, 'people' => 10],
        ];
        $rawValueSeparatorPattern = '[;；、，,]+';
        $outputValueSeparator = '、';
        $valueMapperFn = function ($value) { return trim($value);};
        $valueFilterFn = function ($value) { return mb_strlen($value) > 0;};
        foreach ($rawProjects as $rawProject) {
            echo ".";
            $projectName = $rawProject[2];
            $formDataList = DB::table('applications as a')
                ->leftJoin('application_form_data as b', 'a.id', '=', 'b.id')
                ->select('b.form_data')
                ->where('a.project_name', $projectName)
                ->where('a.status_id', 'E4')
                ->get();
            $count = count($formDataList);
            if ($count !== 1) {
                echo "\n项目名称为 '$projectName' 的项目数为 $count, 不等于 1，退出。";
                exit(-1);
            }

            $outputProject = $rawProject;
            $formData = json_decode($formDataList[0]->form_data, true);
            $basicSituation = $formData['basic_situation'];
            $outputProject[] = $basicSituation['declare_company'];
            $awardLevel = $rawProject[1];

            $completeCompaniesLimit = $completeSubjectCountLimits[$awardLevel]['companies'];
            $completeCompaniesArr = mb_split($rawValueSeparatorPattern, $basicSituation['complete_companies']);
            $completeCompaniesArr = array_map($valueMapperFn, $completeCompaniesArr);
            $completeCompaniesArr = array_values(array_filter($completeCompaniesArr, $valueFilterFn));
            $completeCompaniesArr = array_slice($completeCompaniesArr, 0, $completeCompaniesLimit);
            $completeCompaniesStr = implode($outputValueSeparator, $completeCompaniesArr);
            $completeCompanyCount = count($completeCompaniesArr);

            $completePeopleLimit = $completeSubjectCountLimits[$awardLevel]['people'];
            $completePeopleArr = mb_split($rawValueSeparatorPattern, $basicSituation['complete_people']);
            $completePeopleArr = array_map($valueMapperFn, $completePeopleArr);
            $completePeopleArr = array_values(array_filter($completePeopleArr, $valueFilterFn));
            $completePeopleArr = array_slice($completePeopleArr, 0, $completePeopleLimit);
            $completePeopleStr = implode($outputValueSeparator, $completePeopleArr);
            $completePeopleCount = count($completePeopleArr);

            array_push($outputProject, $completeCompaniesStr, $completeCompanyCount, $completePeopleStr, $completePeopleCount);
            $outputItems[] = $outputProject;
        }

        $awardedProjectsSavePath = __DIR__ . '/output/2020年度中国公路建设行业协会科学技术奖进步奖公示.csv';
        if (write_csv_file($awardedProjectsSavePath, $outputItems)) {
            echo "\nSaved to: $awardedProjectsSavePath\n";
        }
    }
}